var appPath = getAppPath();

$(function() {
	initUserInfoGrid();//初始化加载
	$("#findType").combobox({
		data:[{'id':'-1','name':'不限'},{'id':'0','name':'后台'},{'id':'1','name':'用户'},{'id':'2','name':'kingsoft用户'}],
		valueField:'id',
		textField:'name',
		value:-1
	});
});

//获取用户信息
function initUserInfoGrid() {
	$('#userInfoGrid').datagrid({
		url : appPath + "/itbuser/pageUserInfo.action",//获取用户信息
		height : 'full',
		striped : true,
		fitColumns : true,
		queryParams : getQueryParam1(),
		singleSelect : false,
		selectOnCheck : true,
		checkOnSelect : true,
		columns : [[
		   {field : 'head_url',title : '头像',width : 30,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					var retUrl = '';
	        		if(value==null || value==''){
	        			value = 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/designer/default.jpg';
	        		}
	        		retUrl = '<a href="' +value+ '" target="_blank">'
	        		+ '<div style="display:table;text-align:center;height:80px;width:80px;">'
	        		+ '<span align="center" style="display:table-cell;vertical-align:middle">'
	        		+ '<img src="'+value+'?x-oss-process=image/resize,m_lfit,h_120,w_120'
	        		+ '" style="border-radius:17px;display:inline;max-height:70px;max-width:80px;"/></span></div></a>';
	        		return retUrl;
				}
		   }, 
		   {field : 'user_id',title : '用户ID',width : 30,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return value||'-';
				}
		   },
		   {field : 'user_name',title : '用户账号',width : 35,align : 'center',sortable : false,
			   formatter:function(value,rowData,rowIndex){
				   if(value){
						return '<span style="color:#121212;">'+value+'</span>';
					}
					return '<span style="color:#919191;">*</span>';
			   }
		   },
		   {field : 'nickname',title : '用户昵称', width : 25,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex){
				if(value){
					return '<span style="color:#121212;">'+value+'</span>';
				}
				return '<span style="color:#919191;">-</span>';
				}
			}, 
			{field : 'mobile',title : '手机号',width : 35,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					if(value){
						return '<span style="color:red;">'+value+'</span>';
					}
					return '<span style="color:#919191;">-</span>';
				}
			}, 
			{field : 'sumCnt',title : '买单数',width : 30,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return 'sum:'+value+'<br/>vip:'+rowData.vipCnt+'<br/>doc:'+rowData.tgCnt;
				}
			}, 
			{field : 'sumTotal',title : '金额',width : 40,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return 'sum:'+value/100+'元<br/>vip:'+rowData.vipFee/100+'元<br/>doc:'+rowData.tgFee/100+'元';
				}
			}, 
			{field : 'email',title : '邮箱',width : 30,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					if(value){
						return '<span style="color:red;">'+value+'</span>';
					}
					return '<span style="color:#919191;">-</span>';
				}
			}, 
			{field : 'user_type',title : '类型',width : 20,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					var ret ='<span style="color:red">-</span>';
					switch(Number(value||'0')){
					case 0:ret='<span style="color:#8B7D7B;">后台</span>';break;
					case 1:ret='<span style="color:#8B7765">普通</span>';break;
					case 2:ret='<span style="color:#8B6914">KINGSOFT用户</span>';break;
					}
					return ret;
				}
			}, 
			{field : 'wechat_nickname',title : '微信昵称',width : 30,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return value||'<span style="color:#919191;">-</span>';
				}
			},
//			{field : 'hx_user_name',title : '环信账号',width : 30,align : 'center',sortable : false,
//				formatter : function(value, rowData, rowIndex) {
//					return value||'<span style="color:#919191;">-</span>';
//				}
//			},
			{field : 'edit',title : '编辑',width : 18,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return '<a href="javascript:void(0)" style="font-size:15px;" onclick="showDetail('+rowData.user_id+')">编辑</a>';
				}
			},
			{field : 'create_time',title : '创建时间',width : 50,align : 'center',sortable : false},
			{field : 'update_time',title : '修改时间',width : 50,align : 'center',sortable : false}
			]],
		pagination : true,
		rownumbers : true
	});
}

//获取单个用户信息
function showDetail(id){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#userId").val(id);
	$("#editBtn").show();
	$("#editUserId").attr('disabled','disabled');
	$("#editPass").attr('disabled','disabled');
	$("#editHurl").attr('disabled','disabled');
	$("#editWtoken").attr('disabled','disabled');
	$("#editWUtoken").attr('disabled','disabled');
	$("#editWname").attr('disabled','disabled');
	$("#editHname").attr('disabled','disabled');
	$("#editMobile").attr('disabled','disabled');
	$("#editMail").attr('disabled','disabled');
	$("#editUuid").attr('disabled','disabled');
	$("#editSigna").attr('disabled','disabled');
	
	var url = appPath + '/itbuser/getOneUserInfo.action?id='+id;//获取单个用户信息
	
	$.get(url,function(data){
		if(data && data.length>0){
			var info = data[0];
			$("#editUserId").val(info.user_id);
			$("#editName").val(info.user_name);
			$("#editPass").val(info.password);
			$("#editNickName").val(info.nickname);
			var head_url = 'http://itbour-back.oss-cn-hangzhou.aliyuncs.com/designer/default.jpg';
			if(info.head_url){
				head_url=info.head_url;
			}
			var retUrl = '<a href="' +head_url+ '" target="_blank">'
    		+ '<div style="display:table;text-align:center;height:80px;width:80px;">'
    		+ '<span align="center" style="display:table-cell;vertical-align:middle">'
    		+ '<img src="'+head_url+'?x-oss-process=image/resize,m_lfit,h_120,w_120'
    		+ '" style="display:inline;max-height:80px;max-width:80px;"/></span></div></a>';
			$("#editHurl").html(retUrl);
			$("#editType").combobox({
				data:[{'id':0,'name':'后台'},{'id':1,'name':'用户'},{'id':2,'name':'kingsoft'}],
				valueField:'id',
				textField:'name',
				value:info.user_type
			});
			$("#editWtoken").val(info.wechat_token);
			$("#editWUtoken").val(info.wechat_token_union);
			$("#editWname").val(info.wechat_nickname);
			$("#editHname").val(info.hx_user_name);
			$("#editMobile").val(info.mobile);
			$("#editMail").val(info.email);
			$("#editUuid").val(info.uuid);
			$("#editSigna").val(info.signature);
		}
	});
	
	$('#editDlg').window({
		title : '用户信息',
		iconCls : 'icon-edit',
		modal : true,
		shadow : true,
		collapsible : false,
		minimizable : false,
		maximizable : false
	});
	$('#editDlg').window('move', {
		top : 20,
		left : 20
	});
	$('#editDlg').window('open');
}

//修改
function doEdit(){
	$.messager.confirm('提示信息','是否确定提交修改？',function(r){
		if(r){
			var userId = $("#editUserId").val();//用户ID
			var type = $("#editType").combobox('getValue');
			var uName=$("#editName").val();
			var nickname = $("#editNickName").val();
			var url = appPath+"/itbuser/updUserInfo.action";//修改
			var sendData = {
				"userInfo.user_id": userId||"",
				"userInfo.user_type": type||'',
				"userInfo.user_name": uName || "",
				"userInfo.nickname": nickname || ""
			};
		    $.post(url, sendData, function(data){  
				if (data == "ok") { 
					$.messager.alert('提示信息','提交成功','info');
					doQuery1();
					$("#editDlg").window('close');
		        }else{
		        	$.messager.alert("提示信息","提交失败",'info');
		        	doQuery1();
		        	$("#editDlg").window('close');
		        }
		    });
		}
	});
}

//取消
function cancelEdit(){
	$('#editDlg').window('close');
	$("#editForm").form('clear');
}

//判断时间有效性
function chkInputDate(str){
	var reg = /^(\d+)-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;  
	var r = str.match(reg); 
	if(r==null)return '';  
	r[2]=r[2]-1;  
	var d= new Date(r[1], r[2],r[3], r[4],r[5], r[6]);  
	if(d.getFullYear()!=r[1])return '';  
	if(d.getMonth()!=r[2])return '';  
	if(d.getDate()!=r[3])return '';  
	if(d.getHours()!=r[4])return '';  
	if(d.getMinutes()!=r[5])return '';  
	if(d.getSeconds()!=r[6])return '';  
	return str;  
}


function getQueryParam1() {
	var queryObject = {};
	var mobile=$("#mobile").val();
	if(mobile && mobile.length>0) queryObject.findMobile = mobile;
	var vName = $("#userName").val();
	if(vName && vName.length>0) queryObject.findName = vName;
	var userId = $("#user_id").val();
	if(userId && userId.length>0) queryObject.findId = userId
	var findtype = $("#findType").combobox('getValue');
	var isPay = $("#queryPay").combobox('getValue');
	
	if(Number(isPay)>=0) queryObject.findIsPay = isPay;
	return queryObject;
}

//确定选择用户
function chkUser(){
	var selectItem=$("#userInfoGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择勾选用户！','warning');
		return;
	}
	var itemId = '';
	$.each(selectItem, function(index, item){
		itemId = item.user_id;
	});
	$("#eUserId").val(itemId);
	$("#userInfoDlg").window('close');
	$("#userForm").form('clear');
}

//取消添加会员
function cancelUser(){
	$("#userInfoDlg").window('close');
	$("#userForm").form('clear');	
}

function doQuery1(startFlg) {
	$("#userInfoGrid").datagrid('unselectAll');
	if (startFlg == 1) {
		$("#userInfoGrid").datagrid('load', getQueryParam1());
	} else {
		$("#userInfoGrid").datagrid('reload', getQueryParam1());
	}
}
